
package com.huaweicloud.haydn.delivercore.agent.factory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * @since 2022-12-07
 */
public enum BlockingQueueEnum {
    ARRAY("array"),
    LINKED("linked"),
    SYNC("sync"),
    PRIORITY("priority");

    private final String name;

    BlockingQueueEnum(String name) {
        this.name = name;
    }

    private String getName() {
        return name;
    }

    public BlockingQueue<Runnable> queue(Integer... capacity) {
        int initCapacity = 100;
        if (capacity != null && capacity.length == 1) {
            initCapacity = capacity[0];
        }
        if (name.equals(ARRAY.name)) {
            return new ArrayBlockingQueue<>(initCapacity);
        }
        if (name.equals(LINKED.name)) {
            return new LinkedBlockingDeque<>(initCapacity);
        }
        if (name.equals(SYNC.name)) {
            return new SynchronousQueue<>();
        }
        if (name.equals(PRIORITY.name)) {
            return new PriorityBlockingQueue<>(initCapacity);
        }
        return new SynchronousQueue<>();
    }
}
